package com.summer.mall.controller.admin;

import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.User;
import com.summer.mall.service.OrderService;
import com.summer.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession session;

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表接口
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "list")
    @ResponseBody
    public ResponseService getList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return orderService.manageList(pageNum,pageSize);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 搜索订单接口
     * @param session
     * @param orderno
     * @return
     */
    @PostMapping(value = "search")
    @ResponseBody
    public ResponseService getOrderByOrderNo(HttpSession session, @RequestParam(value = "orderno") Long orderno){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return orderService.manageSearch(orderno);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 获取指定订单的详细信息接口
     * @param session
     * @param orderno
     * @return
     */
    @PostMapping(value = "detail")
    @ResponseBody
    public ResponseService getDetail(HttpSession session, @RequestParam(value = "orderno") Long orderno){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return orderService.manageDetail(orderno);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 发货接口
     * @param session
     * @param orderno
     * @return
     */
    @PostMapping(value = "sendgoods")
    @ResponseBody
    public ResponseService sendGoods(HttpSession session, @RequestParam(value = "orderno") Long orderno){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return orderService.manageSendGoods(orderno);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }
}
